package me.alide.modules.survey.controller;


import me.alide.modules.survey.form.SurveyDetail;
import me.alide.modules.survey.service.OrgInfoService;
import me.alide.modules.survey.service.SurveyService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
@RequestMapping("report")
public class ExportController {


    @Resource
    OrgInfoService orgInfoService;

    @Resource
    SurveyService surveyService;


    @GetMapping("question/export")
    public void questionReport(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SurveyDetail> surveyDetailList = surveyService.queryList(startTime, endTime);
        POIFSFileSystem fs = new POIFSFileSystem(this.getClass().getClassLoader().getResourceAsStream("excel/questionTemp.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);
        // 企业数
        Set<String> orgNames = surveyDetailList.stream().map(SurveyDetail::getOrgName).collect(Collectors.toSet());
        int questions = 0;
        int clearQuestions = 0;
        int type1Quest = 0;
        int type1Clear = 0;
        int type2Quest = 0;
        int type2Clear = 0;
        int type3Quest = 0;
        int type3Clear = 0;
        int type4Quest = 0;
        int type4Clear = 0;
        int type5Quest = 0;
        int type5Clear = 0;
        int type6Quest = 0;
        int type6Clear = 0;
        int type7Quest = 0;
        int type7Clear = 0;
        int type8Quest = 0;
        int type8Clear = 0;
        int type9Quest = 0;
        int type9Clear = 0;
        int type10Quest = 0;
        int type10Clear = 0;
        for (int i = 0; i < surveyDetailList.size(); i++) {
            if(surveyDetailList.get(i).getHasQuestion() != null && surveyDetailList.get(i).getHasQuestion() == 1) { questions += 1; }
            if(surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) {clearQuestions +=1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 1 && surveyDetailList.get(i).getHasQuestion() == 1) { type1Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 1 && surveyDetailList.get(i).getHasQuestion() == 1 && surveyDetailList.get(i).getQuestionStatus() != null &&  surveyDetailList.get(i).getQuestionStatus() == 1) { type1Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 2 && surveyDetailList.get(i).getHasQuestion() == 1) { type2Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 2 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null &&  surveyDetailList.get(i).getQuestionStatus() == 1) { type2Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 3 && surveyDetailList.get(i).getHasQuestion() == 1) { type3Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 3 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type3Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 4 && surveyDetailList.get(i).getHasQuestion() == 1) { type4Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 4 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type4Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 5 && surveyDetailList.get(i).getHasQuestion() == 1) { type5Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 5 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type5Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 6 && surveyDetailList.get(i).getHasQuestion() == 1) { type6Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 6 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type6Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 7 && surveyDetailList.get(i).getHasQuestion() == 1) { type7Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 7 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type7Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 8 && surveyDetailList.get(i).getHasQuestion() == 1) { type8Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 8 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type8Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 9 && surveyDetailList.get(i).getHasQuestion() == 1) { type9Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 9 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type9Clear += 1; }

            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 10 && surveyDetailList.get(i).getHasQuestion() == 1) { type10Quest += 1; }
            if(surveyDetailList.get(i).getQuestionType() != null && surveyDetailList.get(i).getQuestionType() == 10 && surveyDetailList.get(i).getHasQuestion() == 1 &&  surveyDetailList.get(i).getQuestionStatus() != null && surveyDetailList.get(i).getQuestionStatus() == 1) { type10Clear += 1; }
        }

        sheet.getRow(6).getCell(0).setCellValue(orgNames.size());
        sheet.getRow(6).getCell(1).setCellValue(questions);
        sheet.getRow(6).getCell(2).setCellValue(clearQuestions);
        sheet.getRow(6).getCell(3).setCellValue(type1Quest);
        sheet.getRow(6).getCell(4).setCellValue(type1Clear);
        sheet.getRow(6).getCell(5).setCellValue(type2Quest);
        sheet.getRow(6).getCell(6).setCellValue(type2Clear);
        sheet.getRow(6).getCell(7).setCellValue(type3Quest);
        sheet.getRow(6).getCell(8).setCellValue(type3Clear);
        sheet.getRow(6).getCell(9).setCellValue(type4Quest);
        sheet.getRow(6).getCell(10).setCellValue(type4Clear);
        sheet.getRow(6).getCell(11).setCellValue(type5Quest);
        sheet.getRow(6).getCell(12).setCellValue(type5Clear);
        sheet.getRow(6).getCell(13).setCellValue(type6Quest);
        sheet.getRow(6).getCell(14).setCellValue(type6Clear);
        sheet.getRow(6).getCell(15).setCellValue(type7Quest);
        sheet.getRow(6).getCell(16).setCellValue(type7Clear);
        sheet.getRow(6).getCell(17).setCellValue(type8Quest);
        sheet.getRow(6).getCell(18).setCellValue(type8Clear);
        sheet.getRow(6).getCell(19).setCellValue(type9Quest);
        sheet.getRow(6).getCell(20).setCellValue(type9Clear);
        sheet.getRow(6).getCell(21).setCellValue(type1Quest);
        sheet.getRow(6).getCell(22).setCellValue(type10Clear);

        String fileName = "“优化营商环境暨万警助万企”“清问题”工作情况统计表.xls";
        response.reset();
        response.setCharacterEncoding("UTF-8");
        String header = request.getHeader("User-Agent").toUpperCase();
        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
            fileName = URLEncoder.encode(fileName, "utf-8");
            fileName = fileName.replace("+", "%20");
        } else {
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/vnd.ms-excel");
        // 直接用数组缓冲输出流输出
        wb.write(response.getOutputStream());
    }




    @GetMapping("archive/export")
    public void archiveReport(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SurveyDetail> surveyDetailList = surveyService.queryList(startTime, endTime);

        POIFSFileSystem fs = new POIFSFileSystem(this.getClass().getClassLoader().getResourceAsStream("excel/archiveTemp.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);
        // 企业数
        Set<String> orgNames = surveyDetailList.stream().map(SurveyDetail::getOrgName).collect(Collectors.toSet());
        int archives = 0;
        int clearArchives = 0;
        int type1Archives = 0;
        int type1Clear = 0;
        int type2Archives = 0;
        int type2Clear = 0;
        int type3Archives = 0;
        int type3Clear = 0;
        int type4Archives = 0;
        int type4Clear = 0;
        int type5Archives = 0;
        int type5Clear = 0;
        int type6Archives = 0;
        int type6Clear = 0;
        int type7Archives = 0;
        int type7Clear = 0;
        int type8Archives = 0;
        int type8Clear= 0;
        int type9Archives = 0;
        int type9Clear= 0;
        int type10Archives = 0;
        int type10Clear = 0;
        for (int i = 0; i < surveyDetailList.size(); i++) {
            if(surveyDetailList.get(i).getHasArchive() != null && surveyDetailList.get(i).getHasArchive() == 1) { archives += 1; }
            if(surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 11) {clearArchives +=1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 11 && surveyDetailList.get(i).getHasArchive() == 1) { type1Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 11 && surveyDetailList.get(i).getHasArchive() == 1 && surveyDetailList.get(i).getArchiveStatus() != null &&  surveyDetailList.get(i).getArchiveStatus() == 1) { type1Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 12 && surveyDetailList.get(i).getHasArchive() == 1) { type2Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 12 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null &&  surveyDetailList.get(i).getArchiveStatus() == 1) { type2Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 13 && surveyDetailList.get(i).getHasArchive() == 1) { type3Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 13 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type3Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 14 && surveyDetailList.get(i).getHasArchive() == 1) { type4Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 14 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type4Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 15 && surveyDetailList.get(i).getHasArchive() == 1) { type5Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 15 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type5Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 16 && surveyDetailList.get(i).getHasArchive() == 1) { type6Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 16 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type6Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 17 && surveyDetailList.get(i).getHasArchive() == 1) { type7Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 17 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type7Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 18 && surveyDetailList.get(i).getHasArchive() == 1) { type8Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 18 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type8Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 19 && surveyDetailList.get(i).getHasArchive() == 1) { type9Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 19 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type9Clear += 1; }

            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 20 && surveyDetailList.get(i).getHasArchive() == 1) { type10Archives += 1; }
            if(surveyDetailList.get(i).getArchiveType() != null && surveyDetailList.get(i).getArchiveType() == 20 && surveyDetailList.get(i).getHasArchive() == 1 &&  surveyDetailList.get(i).getArchiveStatus() != null && surveyDetailList.get(i).getArchiveStatus() == 1) { type10Clear += 1; }
        }

        sheet.getRow(6).getCell(0).setCellValue(orgNames.size());
        sheet.getRow(6).getCell(1).setCellValue(archives);
        sheet.getRow(6).getCell(2).setCellValue(clearArchives);
        sheet.getRow(6).getCell(3).setCellValue(type1Archives);
        sheet.getRow(6).getCell(4).setCellValue(type1Clear);
        sheet.getRow(6).getCell(5).setCellValue(type2Archives);
        sheet.getRow(6).getCell(6).setCellValue(type2Clear);
        sheet.getRow(6).getCell(7).setCellValue(type3Archives);
        sheet.getRow(6).getCell(8).setCellValue(type3Clear);
        sheet.getRow(6).getCell(9).setCellValue(type4Archives);
        sheet.getRow(6).getCell(10).setCellValue(type4Clear);
        sheet.getRow(6).getCell(11).setCellValue(type5Archives);
        sheet.getRow(6).getCell(12).setCellValue(type5Clear);
        sheet.getRow(6).getCell(13).setCellValue(type6Archives);
        sheet.getRow(6).getCell(14).setCellValue(type6Clear);
        sheet.getRow(6).getCell(15).setCellValue(type7Archives);
        sheet.getRow(6).getCell(16).setCellValue(type7Clear);
        sheet.getRow(6).getCell(17).setCellValue(type8Archives);
        sheet.getRow(6).getCell(18).setCellValue(type8Clear);
        sheet.getRow(6).getCell(19).setCellValue(type9Archives);
        sheet.getRow(6).getCell(20).setCellValue(type9Clear);
        sheet.getRow(6).getCell(21).setCellValue(type10Archives);
        sheet.getRow(6).getCell(22).setCellValue(type10Clear);

        String fileName = "“优化营商环境暨万警助万企”“清挂案”工作情况统计表.xls";
        response.reset();
        response.setCharacterEncoding("UTF-8");
        String header = request.getHeader("User-Agent").toUpperCase();
        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
            fileName = URLEncoder.encode(fileName, "utf-8");
            fileName = fileName.replace("+", "%20");
        } else {
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/vnd.ms-excel");
        // 直接用数组缓冲输出流输出
        wb.write(response.getOutputStream());
    }





    @GetMapping("implied/export")
    public void impliedReport(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SurveyDetail> surveyDetailList = surveyService.queryList(startTime, endTime);

        POIFSFileSystem fs = new POIFSFileSystem(this.getClass().getClassLoader().getResourceAsStream("excel/impliedTemp.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);
        // 企业数
        Set<String> orgNames = surveyDetailList.stream().map(SurveyDetail::getOrgName).collect(Collectors.toSet());
        int implieds = 0;
        int clearImplied = 0;
        int type1Implieds = 0;
        int type1Clear = 0;
        int type2Implieds = 0;
        int type2Clear = 0;
        int type3Implieds = 0;
        int type3Clear = 0;
        int type4Implieds = 0;
        int type4Clear = 0;
        int type5Implieds = 0;
        int type5Clear = 0;
        int type6Implieds = 0;
        int type6Clear = 0;
        int type7Implieds = 0;
        int type7Clear = 0;
        int type8Implieds = 0;
        int type8Clear= 0;
        int type9Implieds = 0;
        int type9Clear= 0;
        int type10Implieds = 0;
        int type10Clear = 0;
        for (int i = 0; i < surveyDetailList.size(); i++) {
            if(surveyDetailList.get(i).getHasImplied() != null && surveyDetailList.get(i).getHasImplied() == 1) { implieds += 1; }
            if(surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 11) {clearImplied +=1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 21 && surveyDetailList.get(i).getHasImplied() == 1) { type1Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 21 && surveyDetailList.get(i).getHasImplied() == 1 && surveyDetailList.get(i).getImpliedStatus() != null &&  surveyDetailList.get(i).getImpliedStatus() == 1) { type1Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 22 && surveyDetailList.get(i).getHasImplied() == 1) { type2Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 22 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null &&  surveyDetailList.get(i).getImpliedStatus() == 1) { type2Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 23 && surveyDetailList.get(i).getHasImplied() == 1) { type3Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 23 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type3Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 24 && surveyDetailList.get(i).getHasImplied() == 1) { type4Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 24 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type4Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 25 && surveyDetailList.get(i).getHasImplied() == 1) { type5Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 25 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type5Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 26 && surveyDetailList.get(i).getHasImplied() == 1) { type6Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 26 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type6Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 27 && surveyDetailList.get(i).getHasImplied() == 1) { type7Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 27 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type7Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 28 && surveyDetailList.get(i).getHasImplied() == 1) { type8Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 28 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type8Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 29 && surveyDetailList.get(i).getHasImplied() == 1) { type9Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 29 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type9Clear += 1; }

            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 30 && surveyDetailList.get(i).getHasImplied() == 1) { type10Implieds += 1; }
            if(surveyDetailList.get(i).getImpliedType() != null && surveyDetailList.get(i).getImpliedType() == 30 && surveyDetailList.get(i).getHasImplied() == 1 &&  surveyDetailList.get(i).getImpliedStatus() != null && surveyDetailList.get(i).getImpliedStatus() == 1) { type10Clear += 1; }
        }

        sheet.getRow(6).getCell(0).setCellValue(orgNames.size());
        sheet.getRow(6).getCell(1).setCellValue(implieds);
        sheet.getRow(6).getCell(2).setCellValue(clearImplied);
        sheet.getRow(6).getCell(3).setCellValue(type1Implieds);
        sheet.getRow(6).getCell(4).setCellValue(type1Clear);
        sheet.getRow(6).getCell(5).setCellValue(type2Implieds);
        sheet.getRow(6).getCell(6).setCellValue(type2Clear);
        sheet.getRow(6).getCell(7).setCellValue(type3Implieds);
        sheet.getRow(6).getCell(8).setCellValue(type3Clear);
        sheet.getRow(6).getCell(9).setCellValue(type4Implieds);
        sheet.getRow(6).getCell(10).setCellValue(type4Clear);
        sheet.getRow(6).getCell(11).setCellValue(type5Implieds);
        sheet.getRow(6).getCell(12).setCellValue(type5Clear);
        sheet.getRow(6).getCell(13).setCellValue(type6Implieds);
        sheet.getRow(6).getCell(14).setCellValue(type6Clear);
        sheet.getRow(6).getCell(15).setCellValue(type7Implieds);
        sheet.getRow(6).getCell(16).setCellValue(type7Clear);
        sheet.getRow(6).getCell(17).setCellValue(type8Implieds);
        sheet.getRow(6).getCell(18).setCellValue(type8Clear);
        sheet.getRow(6).getCell(19).setCellValue(type9Implieds);
        sheet.getRow(6).getCell(20).setCellValue(type9Clear);
        sheet.getRow(6).getCell(21).setCellValue(type10Implieds);
        sheet.getRow(6).getCell(22).setCellValue(type10Clear);

        String fileName = "“优化营商环境暨万警助万企”“清风险”工作情况统计表.xls";
        response.reset();
        response.setCharacterEncoding("UTF-8");
        String header = request.getHeader("User-Agent").toUpperCase();
        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
            fileName = URLEncoder.encode(fileName, "utf-8");
            fileName = fileName.replace("+", "%20");
        } else {
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/vnd.ms-excel");
        // 直接用数组缓冲输出流输出
        wb.write(response.getOutputStream());
    }





    @GetMapping("export")
    public void report(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SurveyDetail> surveyDetailList = surveyService.queryList(startTime, endTime);

        POIFSFileSystem fs = new POIFSFileSystem(this.getClass().getClassLoader().getResourceAsStream("excel/temp7.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.setForceFormulaRecalculation(true);
        fillExcel(surveyDetailList, sheet, wb);
        String fileName = "“万警助万企”“三清”工作明细情况汇总表.xls";
        response.reset();
        response.setCharacterEncoding("UTF-8");
        String header = request.getHeader("User-Agent").toUpperCase();
        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
            fileName = URLEncoder.encode(fileName, "utf-8");
            fileName = fileName.replace("+", "%20");
        } else {
            fileName = new String(fileName.getBytes(), "utf-8");
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("application/vnd.ms-excel");
        // 直接用数组缓冲输出流输出
        wb.write(response.getOutputStream());
    }

    private void fillExcel(List<SurveyDetail> surveyDetailList, HSSFSheet sheet, HSSFWorkbook workbook) {
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        for (int i = 0; i < surveyDetailList.size(); i++) {
            SurveyDetail surveyDetail = surveyDetailList.get(i);
            Row row = sheet.createRow(i + 5);
            for(int j = 0; j < 23; j ++) {
                Cell cell = row.createCell(j);
                cell.setCellStyle(cellStyle);
            }
            sheet.getRow(i + 5).getCell(0).setCellValue(surveyDetail.getOrgName());
            sheet.getRow(i + 5).getCell(1).setCellValue(surveyDetail.getLegal());
            sheet.getRow(i + 5).getCell(2).setCellValue(surveyDetail.getContact());
            sheet.getRow(i + 5).getCell(3).setCellValue(surveyDetail.getContact());
            sheet.getRow(i + 5).getCell(4).setCellValue(surveyDetail.getContactJob());
            sheet.getRow(i + 5).getCell(5).setCellValue(surveyDetail.getContactPhone());
            sheet.getRow(i + 5).getCell(6).setCellValue(surveyDetail.getLeader());
            sheet.getRow(i + 5).getCell(7).setCellValue(surveyDetail.getLeaderJob());
            sheet.getRow(i + 5).getCell(8).setCellValue(surveyDetail.getLeaderPhone());
            sheet.getRow(i + 5).getCell(9).setCellValue(surveyDetail.getLeaderContact());
            sheet.getRow(i + 5).getCell(10).setCellValue(surveyDetail.getLeaderContactJob());
            sheet.getRow(i + 5).getCell(11).setCellValue(surveyDetail.getLeaderContactPhone());
            sheet.getRow(i + 5).getCell(12).setCellValue(surveyDetail.getQuestionName());
            sheet.getRow(i + 5).getCell(13).setCellValue(surveyDetail.getQuestionReason());
            sheet.getRow(i + 5).getCell(16).setCellValue(surveyDetail.getArchiveName());
            sheet.getRow(i + 5).getCell(17).setCellValue(surveyDetail.getArchiveReason());
            sheet.getRow(i + 5).getCell(20).setCellValue(surveyDetail.getImpliedName());
            sheet.getRow(i + 5).getCell(21).setCellValue(surveyDetail.getImpliedReason());
        }
    }

}
